﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;
using SlimDX.Direct3D9;


//這個函式庫其實問題很大，反正幾乎是全部從範例A來的...
//主要要改的地方，包括了elements拉到外面去等等....
namespace LibRadiant.Main
{
    public class Entity : Frame, IDisposable
    {
        public Entity(D3D9Core inContext)
        {
            mContext = inContext;
        }

        protected Device Device
        {
            get { return mContext.D3DDevice; }

        }

        protected D3D9Core mContext;
        private Mesh mMainMesh = null;
        public Mesh MainMesh
        {
            get {
                if (mMainMesh == null)
                    mMainMesh = CreateBaseMesh();

                 return mMainMesh; }
            set { mMainMesh = value; }
        }

        static Material mDefMaterial;
        virtual public Material Material
        {
            get
            {
                return mDefMaterial;
            }
        }

        static public Material DefaultMaterial
        {
            get
            {
                if (mDefMaterial == null)
                    mDefMaterial = CreateDefaultMaterial();

                return mDefMaterial;
            }
        }

        private static Material CreateDefaultMaterial()
        {
            Material theMaterial = new Material();
            theMaterial.Ambient = new Color4(0.2f, 0.2f, 0.2f);
            theMaterial.Diffuse = new Color4(1, 1, 1);
            theMaterial.Power = 0.2f;
            theMaterial.Specular = new Color4(0.7f, 0.7f, 0.7f);
            return theMaterial;
        }

        virtual protected Mesh CreateBaseMesh() { throw new NotImplementedException(); }


        virtual protected void PreRender(){ }
        virtual protected void PostRender() { }
        public void Render()
        {
            PreRender();
            mContext.D3DDevice.Material = Material;
            mContext.D3DDevice.SetTransform(TransformState.World, Matrix);
            MainMesh.DrawSubset(0);
            PostRender();
        }



        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }



        #endregion
    }

}
